limsOutsourceApply.editPage = {

    getGridParam: function () {
        var _this = this
        return {
            type: 'grid',
            id: 'lims-outsource-apply-edit-list-grid',
            url: limsOutsourceApply.baseUrl + '/queries/raw',
            service: 'limsOutsourceApplyServiceImpl',
            dbTable: 'T_LIMS_OUTSOURCE_APPLY',
            columns: limsOutsourceApply.getEditListColumns(),
            onCellClick: function (field, row) {
                field === 'applyCode' && _this.loadDetailPage(row)
            },
            page: true,
            deleteFormatter: function (row) {
                return Gikam.propI18N('T_LIMS_OUTSOURCE_APPLY.APPLYCODE') + ': ' + row.applyCode
            },
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                onClick: function () {
                    // 批量添加
                    /*                    var grid = Gikam.getComp('lims-outsource-apply-edit-list-grid')
                                        Gikam.create('modal', {
                                            width: '80%',
                                            height: '80%',
                                            title: Gikam.choose.getConfig('outsourcer').title,
                                            url: IFM_CONTEXT + Gikam.choose.getConfig('outsourcer').url + Gikam.param({
                                                activedFlag_EQ: '1',
                                                // single: true,
                                            }),
                                            onAfterClose: function (rows) {
                                                if (!Gikam.isEmpty(rows)) {
                                                    var data = rows.map(r => {
                                                        return {
                                                            outsourcerId: r.id,
                                                            outsourcerName: r.outsourcerName,
                                                            outsourcerCode: r.outsourcerCode
                                                        }
                                                    })
                                                    workspace.window.showMask()
                                                    Gikam.postText(limsOutsourceApply.baseUrl + '/insert-multi', Gikam.getJsonWrapper(null, [null, data])).done(req => {
                                                        grid.refresh()
                                                    }).always(function () {
                                                        workspace.window.closeMask()
                                                        Gikam.getLastModal().close()
                                                    })
                                                }
                                            }
                                        })*/
                    Gikam.preInsert({
                        modalTitle: 'BASEMODULE.BUSINESS.OUTSOURCES.MODAL.ADD',
                        fields: limsOutsourceApply.getBaseInfoFormFields(),
                        url: limsOutsourceApply.baseUrl,
                        disableNotEmptyValidate: true,
                    }).done(function (id) {
                        Gikam.getComp('lims-outsource-apply-edit-list-grid').refresh()
                        var row = {
                            id: id
                        }
                        Gikam.getTextSync(Gikam.printf(limsOutsourceApply.baseUrl + '/{id}', {id: id})).done(req => {
                            if (req) {
                                row = JSON.parse(req)
                            }
                        })
                        _this.loadDetailPage(row)
                    })
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                onClick: function () {
                    Gikam.getComp('lims-outsource-apply-edit-list-grid').deleteRows(limsOutsourceApply.baseUrl)
                }
            }, {
                type: 'button',
                text: 'GIKAM.WORKFLOW.BUTTON.SUBMIT',
                icon: 'submit', color: 'success',
                onClick: function () {
                    _this.submit()
                }
            }]
        }
    },

    submit: function () {
        var grid = Gikam.getComp('lims-outsource-apply-edit-list-grid')
        var selections = grid.getSelections()
        if (Gikam.isEmpty(selections)) {
            Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM')
            return
        }
        Gikam.postText(limsOutsourceApply.baseUrl + "/submit-check", Gikam.getJsonWrapper(null, [null, selections])).done(req => {
            req = JSON.parse(req)
            if (req.flag && req.flag === '1') {
                Gikam.create('workflow').submit({
                    data: grid.getSelections(),
                    pageObject: limsOutsourceApply,
                    formatterToolbar: (toolbar, type, formId) => {
                        toolbar.forEach(b => {
                            if (b.text === 'GIKAM.BUTTON.CONFIRM') {
                                b.class = 'blue'
                                b.icon = 'save'
                                b.color = 'white'
                            } else if (b.text === 'GIKAM.BUTTON.CANCEL') {
                                b.icon = 'back'
                            }
                        })
                        return toolbar
                    }
                }).done(function () {
                    grid.refresh()
                })
            } else {
                Gikam.alert(req.message)
            }
        }).always(function () {
        })
    },

    loadDetailPage: function (row) {
        workspace.window.load(limsOutsourceApply.baseUrl + '/page/detail' + Gikam.param({
            id: row.id,
            step: 'edit',
            outsourcerId: row.outsourcerId
        }))
    },

    create: function () {
        var _this = this
        Gikam.create('layout', {
            id: 'lims-outsource-apply-edit-list-layout',
            renderTo: workspace.window.$dom,
            center: {
                items: [this.getGridParam()]
            }
        })
    },

    init: function () {
        this.create()
    }
}
